package com.huiyeji.hui.service.impl;

import com.huiyeji.hui.constant.InvitationConstant;
import com.huiyeji.hui.entity.Invitation;
import com.huiyeji.hui.exception.HuiException;
import com.huiyeji.hui.mapper.InvitationMapper;
import com.huiyeji.hui.service.InvitationService;
import com.huiyeji.hui.utils.ErrorEnum;
import com.huiyeji.hui.vo.InvitationVo;
import com.huiyeji.hui.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaoduo
 */
@Service
public class InvitationServiceImpl implements InvitationService {

    private final InvitationMapper mapper;

    @Autowired
    public InvitationServiceImpl(InvitationMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<InvitationVo> getInvitations(Long receiver) {
        return mapper.getInvitationVoList(receiver);
    }

    @Override
    public List<Invitation> addGroupInvitation(MessageVo vo) {
        String[] fids = vo.getContent().split(",");
        return Arrays.stream(fids).map(fid -> {
            Invitation invitation = new Invitation(
                    null,
                    vo.getSender(),
                    Long.parseLong(fid),
                    vo.getReceiver(),
                    Byte.parseByte("2"),
                    vo.getCreateTime(),
                    Byte.parseByte("0"));
            int row = mapper.insert(invitation);
            if (row != 1){
                mapper.updateCreateTime(invitation);
            }
            return invitation;
        }).collect(Collectors.toList());
    }

    @Override
    public Invitation addFriendInvitation(MessageVo vo) {
        Invitation invitation = new Invitation(
                null,
                vo.getSender(),
                vo.getReceiver(),
                null,
                Byte.parseByte("1"),
                vo.getCreateTime(),
                Byte.parseByte("0"));
        int row = mapper.insert(invitation);
        if (row != 1){
            int row1 = mapper.updateCreateTime(invitation);
            if (row1 != 1){
                throw new HuiException(ErrorEnum.INVITE_ERROR);
            }
        }
        return invitation;
    }

    @Override
    public void refuseInvitation(Long uid, Long id) {
        int row = mapper.changeInvitationStatus(id,uid, InvitationConstant.INVITATION_STATUS_REFUSE);
        if (row != 1){
            throw new HuiException(ErrorEnum.UNABLE_REFUSE_INVITATION);
        }
    }

    @Override
    public void agreeInvitation(Long uid, Long id) {
        int row = mapper.changeInvitationStatus(id,uid, InvitationConstant.INVITATION_STATUS_AGREE);
        if (row != 1){
            throw new HuiException(ErrorEnum.UNABLE_AGREE_INVITATION);
        }
    }

    @Override
    public Invitation getInvitation(Long id) {
        return mapper.getInvitationById(id);
    }

    @Override
    public void deleteAllGroupInvitation(Long gid) {
        mapper.deleteInvitationsByGid(gid);
    }

    @Override
    public void delete(Long uid, Long id) {
        int row = mapper.deleteById(uid,id);
        if (row != 1){
            throw new HuiException(ErrorEnum.DELETE_ERROR);
        }
    }
}
